//
//  TPSetPasswordViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/11/2.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPSetPasswordViewController.h"

@interface TPSetPasswordViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *oldTextFiled;
@property (nonatomic,strong)UITextField *textFiled;
@property (nonatomic,strong)UITextField *suqTextFiled;
@property (nonatomic,assign)BOOL isSet;
@end

@implementation TPSetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isSet = ![LoginModel shareLoginModel].isSetPsw;
    self.title = _isSet ? @"设置密码":@"修改密码";
    
    UIImageView *view = [[UIImageView alloc]init];
    view.userInteractionEnabled = YES;
    view.clipsToBounds = YES;
    view.contentMode = UIViewContentModeScaleAspectFill;
//    view.image = ImageNamed(@"TP_Password_textField_bg");
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(IOS7_TOP_Y);
        make.left.bottom.right.mas_equalTo(0.0f);
    }];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:ImageNamed(@"TP_login_update_phone_header")];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20.0);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(222, 117));
    }];
    
    
    _oldTextFiled = [self setup_placeholder:@"请输入旧密码" leftImage:@"TP_Password_textField_image" subView:view];
    [_oldTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageView.mas_bottom).offset(20.0);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.8);
        make.height.mas_equalTo( 38.0f);
        make.centerX.mas_equalTo(self.view);
    }];
    _oldTextFiled.hidden = _isSet;
    
    _textFiled = [self setup_placeholder:@"请输入新密码" leftImage:@"TP_Password_textField_icon" subView:view];
    [_textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oldTextFiled.mas_bottom).offset(20.0);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.8);
        make.height.mas_equalTo(38.0f);
        make.centerX.mas_equalTo(self.view);
    }];
    
    _suqTextFiled = [self setup_placeholder:@"请确认密码" leftImage:@"TP_Password_textField_sub_icon" subView:view];
    [_suqTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textFiled.mas_bottom).offset(20.0);
        make.width.height.mas_equalTo(self.textFiled);
        make.centerX.mas_equalTo(self.view);
    }];
    
    [self.view layoutIfNeeded];
    
    @weakify(self);
    TPButton *submitButton = [TPButton buttonWithTitle:@"保存" toHeight:38.0 ActionHandler:^(NSInteger tag, UIButton *button) {
        @strongify(self);
        
        [self.view endEditing:YES];
        
        if(self.isSet){
            [self getSetRequest];
        }else{
            [self getUpdateRequest];
        }
        
        
    }];
    [submitButton tp_arrow];
    [view addSubview:submitButton];
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_suqTextFiled.mas_bottom).offset(50.0);
        make.height.mas_equalTo(38.0f);
        make.width.mas_equalTo(self.view).multipliedBy(0.5);
        make.centerX.mas_equalTo(self.view);
    }];
    
}


-(void)getUpdateRequest{
    
    
    if(![self.textFiled.text isEqualToString:self.suqTextFiled.text]){
        [self.view xw_hideTitle:@"两次输入密码不正确"];
        return;
    }else if(self.textFiled.text.length == 0){
        [self.view xw_hideTitle:@"请输入密码"];
        return;
    }else if (self.oldTextFiled.text.length == 0){
        [self.view xw_hideTitle:@"请输入旧密码"];
        return;
    }
    [UIView xw_showHUDWithTitle:@"修改中..."];
    [[TPRequestClient sharedClient]getUpdOldPsw:_oldTextFiled.text toPass:_textFiled.text success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        [LoginModel shareLoginModel].pass = _textFiled.text;
        [LoginModel shareLoginModel].isSetPsw = YES;
         [[LoginModel shareLoginModel]save];
          [UIView xw_hideTitle:@"修改成功.."];
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        
        [UIView xw_hideTitle:respone.message];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [UIView xw_hideTitle:@"Network Error"];
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)getSetRequest{
    
    
    if([self.textFiled.text isEqualToString:self.suqTextFiled.text] && self.textFiled.text.length >0){
        
    }else{
        [self.view xw_hideTitle:@"两次输入密码不正确"];
        return;
    }
    [UIView xw_showHUDWithTitle:@"设置中..."];
    [[TPRequestClient sharedClient]getEditPsw:_textFiled.text success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        [LoginModel shareLoginModel].pass = _textFiled.text;
        [LoginModel shareLoginModel].isSetPsw = YES;
        [[LoginModel shareLoginModel]save];
        
        [UIView xw_hideTitle:@"设置成功.."];
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        
        [UIView xw_hideTitle:respone.message];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [UIView xw_hideTitle:@"Network Error"];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
//    [self.textFiled becomeFirstResponder];
}
-(UITextField *)setup_placeholder:(NSString *)placeholder leftImage:(NSString *)image subView:(UIView *)subView{
     UITextField*textFiled = [UITextField initWithFrame:CGRectZero borderStyle:UITextBorderStyleNone fontSize:16 placeholder:placeholder text:@""];
    textFiled.delegate = self;
    [textFiled xw_addLeftImageViewFrame:CGRectMake(0, 0, 40.0, 40.0) image:[ImageNamed(image) imageWithTintColor:TPAllBackgroundColor] imageFrame:CGRectMake(10, 10, 20.0, 20.0)];
//    [textFiled setPlaceholderLabelTextColor:[UIColor whiteColor]];
    textFiled.textColor = [UIColor darkGrayColor];
    textFiled.secureTextEntry = YES;
    [textFiled tp_addLineBottomWithColor:TP_COLOR_240];
    [subView addSubview:textFiled];
    return textFiled;
    
}


@end
